package com.coc.controller.system;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coc.core.utils.CheckObjField;
import com.coc.core.utils.StringRelaUtils;
import com.coc.entity.core.JsonResult;
import com.coc.entity.core.ResultTool;
import com.coc.entity.system.SysUser;
import com.coc.entity.system.SysUserRole;
import com.coc.service.system.SysUserService;
import com.coc.service.system.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户表(SysUser)表控制层
 *
 * @author songju
 * @since 2020-02-10 19:15:22
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController extends ApiController {

    /**
     * 服务对象
     */
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    public JsonResult selectAll(Page<SysUser> page, SysUser sysUser) {
        QueryWrapper<SysUser> sqw = new CheckObjField<SysUser>().getFields(sysUser);
        Page<SysUser> page1 = sysUserService.page(page,sqw);
        return ResultTool.success(page1);
    }
    @PostMapping("/addUser")
    public JsonResult addUser(@RequestBody String json) {
        JsonResult jsonResult = sysUserService.addUser(json);
        return ResultTool.success();
    }
    @PostMapping("/getRoleByUser")
    public JsonResult getRoleByUser(@RequestBody String json) {
        JsonResult result = ResultTool.success();
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer user_id = jsonObject.getInteger("user_id");
        QueryWrapper<SysUserRole> spq = new QueryWrapper<>();
        spq.eq("user_id",user_id);
        List<SysUserRole> list = sysUserRoleService.list(spq);
        result.setData(list);
        return result;
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PostMapping("/delBatchByIds")
    public JsonResult delBatchByIds(@RequestBody String ids){
        JSONObject jsonObject = JSONObject.parseObject(ids);
        String idss = jsonObject.getString("id");
        List<String> ss = StringRelaUtils.strToList(idss);
        QueryWrapper<SysUserRole> sqw = new QueryWrapper<>();
        sqw.in("user_id",ss);
        //删除sys_user_role表中与该用户对应的关系
        boolean remove = sysUserRoleService.remove(sqw);
        //删除该用户,也可以逻辑删除,即把is_del状态改为1,这里就直接删除了,记着把查询用户的那时添加条件哦
        boolean b = sysUserService.removeByIds(ss);
        if (b&&remove){
            return ResultTool.success("删除成功");
        }else {
            return ResultTool.fail();
        }
    }
    @PostMapping("/editUser")
    public JsonResult editUser(@RequestBody String json) {

        JsonResult jsonResult = sysUserService.editUser(json);
        return ResultTool.success();
    }
}